﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace FloatBall
{
    class Player
    {
        //Public constants
        

        //Public variables and objects
        public bool isActive;
        public bool isAlive;
        public bool isLocal;
        public bool isInTeamOne;
        public Vector3 position;
        public Vector3 velocity;
        public float rot;
        public float rotSpeed;
        public float pitch;
        public float pitchSpeed;
        public float roll;
        public float rollSpeed;
        public BoatTypes.BoatType type;


        //Private constants

        //Private variables and objects
        Vector3 dirFront;
        Vector3 dirLeft;


        //Constructor
        public Player()
        {
            position = new Vector3(5f, -0.03f, 5f);
            velocity = Vector3.Zero;
            isActive = false;
            isAlive = false;
            isLocal = true;
            isInTeamOne = true;
            rot = 0;
            type = BoatTypes.boatType[0];
            
        }
        
        public Player(int newBoatType, Vector3 newPos, bool newIsInTeamOne, bool newIsLocal)
        {
            position = newPos;
            velocity = Vector3.Zero;
            isActive = true;
            isAlive = true;
            isLocal = newIsLocal;
            isInTeamOne = newIsInTeamOne;
            type = BoatTypes.boatType[newBoatType];
            rot = 0;
            rotSpeed = 0;
            pitch = 0;
            pitchSpeed = 0;
            roll = 0;
            rollSpeed = 0;

        }


        //Class methods


        public void Update(bool up, bool left, bool right)
        {
            rot += rotSpeed;
            dirFront = new Vector3((float)Math.Sin((double)rot), 0, (float)Math.Cos((double)rot));
            dirLeft = new Vector3(dirFront.Z, 0, -dirFront.X);

            //totalDisp berättar hur mycket vattenkontakt båten har. Water.ApplyBoyancy räknar ut och applicerar all växelverkan mellan båt och vatten.
            float totalDisp = Water.ApplyBoyancy(ref position, ref velocity, ref pitchSpeed, ref pitch, ref rollSpeed, ref roll, ref rot, ref type, ref dirFront);


            if (totalDisp > 0.1f)
            {
                if (left) rotSpeed += type.turnSpeed;
                if (right) rotSpeed -= type.turnSpeed;

                rotSpeed *= 0.85f;

                if (up && totalDisp > 0.1f)
                {
                    velocity += dirFront * (type.enginePower);

                    Effects.CreateParticle(position - (0.40f * type.length + 5 * Effects.FastRand.GetFloat()) * dirFront, new Vector3(0, 3.4f, 0) + 0.5f * velocity, new Vector3(0.2f));
                    Effects.CreateParticle(position - (0.36f * type.length + Effects.FastRand.GetFloat()) * dirFront, new Vector3(0, 0.6f, 0), new Vector3(0.4f));
                    //Effects.CreateParticle(position - (0.34f * type.length + Effects.FastRand.GetFloat()) * dirFront, new Vector3(0, 0.6f, 0), new Vector3(0.4f));
                    Effects.CreateParticle(position - (0.32f * type.length + Effects.FastRand.GetFloat()) * dirFront, new Vector3(0, 0.5f, 0), new Vector3(0.4f));
                    Effects.CreateParticle(position - (0.30f * type.length + Effects.FastRand.GetFloat()) * dirFront, new Vector3(0, 0.4f, 0), new Vector3(0.7f));
                    //Effects.CreateParticle(position - (0.38f * type.length + Effects.FastRand.GetFloat()) * dirFront, new Vector3(0, 0.4f, 0), new Vector3(0.7f));
                }
            }
            
            velocity.Y += Constants.GRAVITY;
            
            
            


            position += velocity;



            //Skapa vattenstänk från skrovet
            float splash = (float)Math.Sqrt(velocity.Length() * totalDisp * 0.4f);
            int splashParticles = (int)MathHelper.Clamp(splash * 3f - 5, 0, 7);

            for (int n = 0; n < splashParticles; n++)
                Effects.CreateParticle(new Vector3(position.X, position.Y +5, position.Z), new Vector3(0, 0.2f, 0) + velocity, new Vector3(splash, 0.1f, splash));
            
        }

        


    }
}
